import random
from urllib import request, parse
import socket
import time
import math
import multiprocessing
from bs4 import BeautifulSoup
from cqssc.db.lottery_number import LotteryNumber
from cqssc.db.log import Log
from cqssc.db.location_number import LocationNumber
from cqssc.buy.big_or_small import BigOrSmall,ForecastBig

class TuGanJue:
    # "http://www.lecai.com/lottery/draw/sorts/ajax_get_draw_data.php?lottery_type=200&date=2017-06-04"
    url = r"http://tb.tuganjue.com/ssc/php/wxkj.php?r={date}"
    response_date = None
    header = {}
    response = None
    post_params = {}
    lottery_data = []
    pagesize = 30
    log = None
    start_time = 0
    execute_time = 0

    def __add_header(self):
        self.header['X-Requested-With'] = r"XMLHttpRequest"
        self.header['Referer'] = r"http://tb.tuganjue.com/ssc/wxkj.html"
        self.header['User-Agent'] = r"""
            Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400
            """
        self.header['Origin'] = r"http://tb.tuganjue.com"
        self.log = Log()

    def __init__(self):
        self.date = random.random()
        self.url = self.url.format(date=self.date)
        self.__add_header()
        self.post_params['pagesize'] = self.pagesize
        self.post_params['page'] = 1
        self.post_params['order'] = 1
        self.post_params['code'] = 1
        self.start_time = time.time()
        socket.timeout = 30

    def collection(self):
        data = parse.urlencode(self.post_params)
        req = request.Request(self.url, data=data.encode('utf-8'))

        for k in self.header:
            req.add_header(k, self.header[k])

        try:
            self.response = request.urlopen(req, timeout=5)
            self.response_date = self.response.read()
            self.response.close()
        except Exception as e:
            print(e)
            self.log.addLog(collection_source="tuganjue", collection_num=0, add_num=0,
                            execute_time=0, level=1, message="Connection Timeout")
            return False
        except socket.error as e:
            print(e)
            self.log.addLog(collection_source="tuganjue", collection_num=0, add_num=0,
                            execute_time=0, level=1, message="Socket Error")
            return False
        except socket.timeout as e:
            print(e)
            self.log.addLog(collection_source="tuganjue", collection_num=0, add_num=0,
                            execute_time=0, level=1, message="Socket Timeout")
            return False

        bs = BeautifulSoup(self.response_date, 'html.parser')
        first_data = bs.find("tbody", attrs={"id": "body"})

        if first_data is None:
            print("采集错误！")

        for tr in first_data.children:
            if tr.name == "tr":
                tds = tr.find_all("td")
                period = tds[0].text
                number = tds[1].text
                lottery_data = {"period": period, "number": number}
                self.lottery_data.append(lottery_data)

        self.lottery_data.reverse()
        collection_num = self.lottery_data.__len__()
        print("本次采集：{num}条数据".format(num=collection_num))
        print("采集完成")
        save_num = self.save()
        execute_time = int((time.time() - self.start_time) * 1000)
        # 记录日志
        self.log.addLog(collection_source="tuganjue", collection_num=collection_num, add_num=save_num,
                        execute_time=execute_time, level=0)
        return self

    def save(self):
        db = LotteryNumber()
        save_num = 0
        multiprocessing.freeze_support()

        for lottery in self.lottery_data:
            period = lottery['period']
            # print("{period}:{number}".format(period=period,number=lottery['number']))
            if not db.hasPeriod(period):
                save_num += 1
                id = db.insertLottery(lottery)
                print("入库{period}:{number}".format(period=period, number=lottery['number']))
                # 分析5个位置数据
                self.multiprocessAnalyzeNum(lottery)
                #预测买大小
                self.multiprocessForecast(lottery)

        db.close()
        self.lottery_data.clear()
        print("入库成功，成功入库{num}条".format(num=save_num))
        return save_num

    def multiprocessForecast(self,lottery):
        forecast_first = multiprocessing.Process(target=self.forecaset, args=(lottery, 1))
        forecast_first.start()
        forecast_two = multiprocessing.Process(target=self.forecaset, args=(lottery, 2))
        forecast_two.start()
        forecast_three = multiprocessing.Process(target=self.forecaset, args=(lottery, 3))
        forecast_three.start()
        forecast_four = multiprocessing.Process(target=self.forecaset, args=(lottery, 4))
        forecast_four.start()
        forecast_five = multiprocessing.Process(target=self.forecaset, args=(lottery, 5))
        forecast_five.start()

    def forecaset(self,lottery,location):
        big = BigOrSmall(lottery=lottery,location=location)
        big.lottery()

    # 分析5个位置数据
    def multiprocessAnalyzeNum(self,lottery):
        first_num = multiprocessing.Process(target=self.analyzeLocationNum, args=(lottery, 1, 'first_number'))
        first_num.start()
        second_number = multiprocessing.Process(target=self.analyzeLocationNum, args=(lottery, 2, 'second_number'))
        second_number.start()
        third_number = multiprocessing.Process(target=self.analyzeLocationNum, args=(lottery, 3, 'third_number'))
        third_number.start()
        fourth_number = multiprocessing.Process(target=self.analyzeLocationNum, args=(lottery, 4, 'fourth_number'))
        fourth_number.start()
        fifth_number = multiprocessing.Process(target=self.analyzeLocationNum, args=(lottery, 5, 'fifth_number'))
        fifth_number.start()

    # 开启进程分析第一位
    def analyzeLocationNum(self, lottery_data,location,table):
        first_num = LocationNumber(location=location,table=table)
        first_num.insert(lottery_data)
        first_num.close()


if __name__ == "__main__":
    t = TuGanJue()
    t.pagesize = 100
    t.collection()
